package com.itheima.wisdomnext.service.ImpI;

import com.itheima.wisdomnext.Handler.WebSocketHandler;
import com.itheima.wisdomnext.entity.Announcement;
import com.itheima.wisdomnext.entity.Result.enumcalss.ErrorCode;
import com.itheima.wisdomnext.mapper.AnnouncementMapper;
import com.itheima.wisdomnext.service.AnnouncementService;
import com.itheima.wisdomnext.util.AnnouncementS_Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;


@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;

    @Autowired
    private WebSocketHandler webSocketHandler;

    private static final String UPLOAD_DIR = "uploads/announcements/";  // 图片文件存储路径

    // 获取所有公告
    @Override
    public List<Announcement> getAllAnnouncements() {
        List<Announcement> allAnnouncements = announcementMapper.findAllAnnouncements();
        if (allAnnouncements == null) {
            AnnouncementS_Util.isTrue(true,ErrorCode.FAILED,"获取所有公告失败");
        }
        return announcementMapper.findAllAnnouncements();
    }

    // 根据 ID 获取公告
    @Override
    public Announcement getAnnouncementById(Integer id) {
        Announcement announcementById = announcementMapper.findAnnouncementById(id);
        if (announcementById == null) {
            AnnouncementS_Util.isTrue(true,ErrorCode.FAILED,"根据 ID 获取公告失败");
        }
        return announcementMapper.findAnnouncementById(id);
    }

    // 发布公告
    @Override
    public void createAnnouncement(Announcement announcement) throws Exception{
//        // 如果没有上传文件，抛异常
//        if (files == null || files.length == 0) {
//            AnnouncementS_Util.isTrue(true, ErrorCode.PARAMS_ERROR, "没有上传文件");
//        }
//
//        // 处理上传的第一张图片
//        String fileUrl = uploadFiles(files[0]);
//        announcement.setImageUrl(fileUrl);

        // 保存公告到数据库
        announcementMapper.insertAnnouncement(announcement);

        webSocketHandler.broadcastAnnouncementToAllUsers(announcement.getTitle()+ ":" +announcement.getContent());

    }


    // 上传单个文件（图片）
    private String uploadFiles(MultipartFile file) throws Exception{
            // 确保存储目录存在
            File directory = new File(UPLOAD_DIR);
            if (!directory.exists()) {
                directory.mkdirs();
            }

            String originalFilename = file.getOriginalFilename();
            String newFilename = System.currentTimeMillis() + "_" + originalFilename;

            // 生成文件存储路径
            Path path = Paths.get(UPLOAD_DIR + newFilename);
            Files.copy(file.getInputStream(), path);


            // 存储文件的 URL
            return UPLOAD_DIR + newFilename;
    }

    // 更新公告
    @Override
    public void updateAnnouncement(Announcement announcement) throws Exception{
        announcementMapper.updateAnnouncement(announcement);
    }

    // 将公告状态改为失效（软删除）
    @Override
    public void deactivateAnnouncement(Integer id) throws Exception{
        announcementMapper.deactivateAnnouncement(id);
    }
}
